package com.confinderence;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.confinderence.model.Lecturer;
import com.confinderence.model.User;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;

public class Utils {
	private static final String userSettings = "userSettings";
	private static final String IS_LOGGED_IN = "loggedIn";
	private static final String LOGGED_USERNAME = "loggedUsername";
	public static final String IP = "192.168.0.105:8080";
	public static final String REST_URL = "http://" + IP + "/ConferenceManager/rest";
	public static final String TRANS_CONF_KEY = "{\"transferConference\":";
	public static Lecturer mLecturer;
	public static boolean checkLoggedIn(Context context) {
		SharedPreferences sp = context.getSharedPreferences(userSettings, Context.MODE_PRIVATE);
		return sp.getBoolean(IS_LOGGED_IN, false);
	}
	public static void signInUser(User user, Context context) {
		SharedPreferences sp = context.getSharedPreferences(userSettings, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(LOGGED_USERNAME, user.getUsername());
		editor.putBoolean(IS_LOGGED_IN, true);
		editor.commit();
	}
	public static void setLecturer(Lecturer lecturer) {
		mLecturer = lecturer;
	}
	public static Lecturer getLecturer() {
		return mLecturer;
	}
	// public static String cleanDateFormat(String json) { // takes in a string
	// of
	// // JSON
	// Pattern regex =
	// Pattern.compile("\\d\\d:\\d\\d:\\d\\d[-\\+]\\d\\d:\\d\\d");
	// Matcher regexMatcher = regex.matcher(json);
	// StringBuffer buff = new StringBuffer();
	// while (regexMatcher.find()) {
	// regexMatcher.appendReplacement(buff, getSubOfMatch(regexMatcher));
	// }
	// regexMatcher.appendTail(buff);
	// return buff.toString();
	// }
	// then pull out the colon.
	public static String getSubOfMatch(Matcher matcher) {
		StringBuilder sb = new StringBuilder(matcher.group(0));
		sb.deleteCharAt(sb.length() - 3);
		return sb.toString();
	}

	protected static void goToConferences(Activity currentActivity) {
		Intent intent = new Intent(currentActivity, ConferenceActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		currentActivity.startActivity(intent);
	}

	protected static void logout(Activity currentActivity) {
		Intent intent = new Intent(currentActivity, MainActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		SharedPreferences sp = currentActivity.getSharedPreferences(userSettings,
				Context.MODE_PRIVATE);
		mLecturer = null;
		Editor editor = sp.edit();
		editor.putString(LOGGED_USERNAME, "");
		editor.putBoolean(IS_LOGGED_IN, false);
		currentActivity.startActivity(intent);
	}

	protected static void goToProfile(Activity currentActivity) {
		Intent intent = new Intent(currentActivity, ProfileActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		currentActivity.startActivity(intent);
	}

	protected static void goToMessages(Activity currentActivity) {
		Intent intent = new Intent(currentActivity, MessageActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		currentActivity.startActivity(intent);
	}

	protected static void goToHome(Activity currentActivity) {
		logout(currentActivity);
	}

	public static void initMenu(Menu menu, final Activity activity) {
		MenuItem homeItem = menu.getItem(1);
		homeItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Utils.goToHome(activity);
				return true;
			}
		});
		MenuItem conferencesItem = menu.getItem(2);
		conferencesItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Utils.goToConferences(activity);
				return true;
			}
		});
		MenuItem messagesItem = menu.getItem(3);
		messagesItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Utils.goToMessages(activity);
				return true;
			}
		});
		MenuItem profileItem = menu.getItem(4);
		profileItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Utils.goToProfile(activity);
				return true;
			}
		});
		MenuItem logoutItem = menu.getItem(5);
		logoutItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Utils.logout(activity);
				return true;
			}
		});
	}
}
